<?
//<meta http-equiv="content-type" content="text/html; charset=utf-8">
include_once "head.php";
include_once "umls_toolbox.php";
?>
<p><a href="index.php">HOME</a> / <a href="setup.php">Setup</a> / <a href="install.php">UMLS Toolbox Install</a> / <strong>Process</strong></p>
<?
print_debug_msg("process started");

if ( $cmd == "init_db" ) {
	$query_array = explode(";", $init_query);
	foreach ( $query_array as $q ) {
		if ( !trim($q) ) continue;
		mysql_query($q) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	}

} else if ( $cmd == "init_program" ) {


	$default_cui_str_list["S0000001"] = "Single Concept";
	
	print_debug_msg("<strong>1. Concept Tree</strong>");

	print_debug_msg("truncate");
	$query_delete = "truncate {$table_prefix}concept_tree";
	$result_delete = mysql_query($query_delete) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	
	foreach ( $default_cui_str_list as $cui => $str ) {
		$query_insert = sprintf("insert into {$table_prefix}concept_tree(cui,str,fcui,fstr,depth,child_cnt) values ('%s','%s','%s','%s','%s','%s')", 
			$cui, add_to_query($str), $cui, add_to_query($str), "1", "0");
		$result_insert = mysql_query($query_insert) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	}

	print_debug_msg("disable keys");
	$query = "alter table {$table_prefix}concept_tree disable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());
	
	print_debug_msg("create cui_ostr_list");
	$cui_ostr_list = array();
	$query = "select cui, str from MRCONSO where lat = 'ENG' and ts = 'P' and stt = 'PF' AND ispref = 'Y'"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui = $row['cui'];
		$cui_ostr_list[$cui] = $row['str'];
	}
	mysql_free_result($result);
	
	print_debug_msg("create aui_cui_list");
	$aui_cui_list = array();
	$query = "select aui, cui from MRHIER"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$aui = $row['aui'];
		$aui_cui_list[$aui] = $row['cui'];
	}
	mysql_free_result($result);

	print_debug_msg("create cui_rn_list");
	$cui_rn_list = array();
	$query = "select cui1, cui2 from MRREL where rel = 'RN'"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui1 = $row['cui1'];
		$cui2 = $row['cui2'];
		$cui_rn_list[$cui1][$cui2] = true;
	}
	mysql_free_result($result);

	print_debug_msg("create fcui_list");
	$fcui_list = array();
	$query = "select aui, ptr from MRHIER"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$aui = $row['aui'];
		$ptr = $row['ptr'];

		$aui_array = array();
		if ( $ptr ) $aui_array = explode(".", $ptr);
		$aui_array[] = $aui;

		$fcui = "";
		foreach ( $aui_array as $aui ) {
			$cui = $aui_cui_list[$aui];
			if ( $fcui ) $fcui .= "_";
			$fcui .= $cui;
		}
		$fcui_list[$fcui] = true;
		
		if ( $cui_rn_list[$cui] ) {
			foreach ( $cui_rn_list[$cui] as $cui2 => $val ) {
				$sfcui = $fcui;
				if ( $sfcui ) $sfcui .= "_";
				$sfcui .= $cui2;
				if ( !isset($fcui_list[$sfcui]) ) $fcui_list[$sfcui] = false;
			}
		}
	}
	mysql_free_result($result);

	print_debug_msg("create child_cnt_list");
	$child_cnt_list = array();
	foreach ( $fcui_list as $fcui => $val ) {
		$idx = "";
		foreach ( explode("_", $fcui) as $c ) {
			if ( $idx ) $idx .= "_";
			$idx .= $c;
			if ( $fcui == $idx ) break;
			$child_cnt_list[$idx]++;
		}
	}
	
	print_debug_msg("insert concept_tree");
	$cui_child_cnt_list = array();
	foreach ( $fcui_list as $fcui => $val ) {
	
		$str = "";
		$fstr = "";
		$depth = 0;
		foreach ( explode("_", $fcui) as $cui ) {
			if ( !$cui ) continue;
			$str = $cui_ostr_list[$cui];
			if ( $fstr ) $fstr .= "_";
			$fstr .= $str;
			$depth++;
		}
		$child_cnt = $child_cnt_list[$fcui];
		if ( $cui_child_cnt_list[$cui] < $child_cnt ) $cui_child_cnt_list[$cui] = $child_cnt;

		$query_insert = sprintf("insert into {$table_prefix}concept_tree(cui,str,fcui,fstr,depth,child_cnt) values ('%s','%s','%s','%s','%s','%s')", 
			$cui, add_to_query($str), $fcui, add_to_query($fstr), $depth, $child_cnt_list[$fcui]);
		$result_insert = mysql_query($query_insert) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	}
	
	print_debug_msg("enable keys");
	$query = "alter table {$table_prefix}concept_tree enable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());
	
	unset($aui_cui_list);
	unset($cui_rn_list);
	unset($fcui_list);
	unset($child_cnt_list);

	print_debug_msg("<strong>2. Concept</strong>");

	print_debug_msg("truncate");
	$query_delete = "truncate {$table_prefix}concept";
	$result_delete = mysql_query($query_delete) or die("\n<br>Query:$query\n<br>Error:".mysql_error());

	foreach ( $default_cui_str_list as $cui => $str ) {
		$query_insert = sprintf("insert into {$table_prefix}concept(cui,str,str_list,sab_list,sty_list,ro_cnt,rq_cnt,sy_cnt,child_cnt) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')", 
			$cui, add_to_query($str), add_to_query($str), "SYSTEM", "", "0", "0", "0", "0");
		$result_insert = mysql_query($query_insert) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	}

	print_debug_msg("disable keys");
	$query = "alter table {$table_prefix}concept disable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());

	print_debug_msg("create cui_str_list & cui_sab_list");
	$cui_str_list = array();
	$cui_sab_list = array();
	$query = "select cui, str, sab from MRCONSO"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui = $row['cui'];
		$str = $row['str'];
		$sab = $row['sab'];
		$cui_str_list[$cui] .= "_".$str;
		$cui_sab_list[$cui] .= "_".$sab;
	}
	mysql_free_result($result);

	print_debug_msg("create cui_sty_list");
	$cui_tui_list = array();
	$tui_sty_list = array();
	$query = "select cui, tui, sty from MRSTY"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui = $row['cui'];
		$tui = $row['tui'];
		$sty = $row['sty'];
		$cui_tui_list[$cui] .= "_".$tui;
		$tui_sty_list[$tui] = $sty;
	}
	mysql_free_result($result);

	print_debug_msg("create cui_ro_list, cui_rq_list, cui_sy_list");
	$cui_ro_list = array();
	$cui_rq_list = array();
	$cui_sy_list = array();
	$query = "select cui1, rel, cui2 from MRREL"; 
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui1 = $row['cui1'];
		$rel = $row['rel'];
		$cui2 = $row['cui2'];
		
		if ( $rel == "RO" ) $cui_ro_list[$cui1] .= "_".$cui2;
		else if ( $rel == "RQ" ) $cui_rq_list[$cui1] .= "_".$cui2;
		else if ( $rel == "SY" ) $cui_sy_list[$cui1] .= "_".$cui2;
	}
	mysql_free_result($result);

	print_debug_msg("insert concept");
	foreach ( $cui_str_list as $cui => $cui_val ) {
	
		$str = $cui_ostr_list[$cui];
		
		$str_list = make_distinct_item_str($cui_val);
		$sab_list = make_distinct_item_str($cui_sab_list[$cui]);
		$sty_list = make_distinct_item_sty($cui_tui_list[$cui]);
		
		$ro_cnt = make_distinct_item_count($cui_ro_list[$cui]);
		$rq_cnt = make_distinct_item_count($cui_rq_list[$cui]);
		$sy_cnt = make_distinct_item_count($cui_sy_list[$cui]);
		
		$child_cnt = $cui_child_cnt_list[$cui];

		$query_insert = sprintf("insert into {$table_prefix}concept(cui,str,str_list,sab_list,sty_list,ro_cnt,rq_cnt,sy_cnt,child_cnt) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s')", 
			$cui, add_to_query($str), add_to_query($str_list), $sab_list, $sty_list, $ro_cnt, $rq_cnt, $sy_cnt, $child_cnt);
		$result_insert = mysql_query($query_insert) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	}

	print_debug_msg("enable keys");
	$query = "alter table {$table_prefix}concept enable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());

	print_debug_msg("<strong>3. Concept nstr</strong>");

	print_debug_msg("truncate");
	$query_delete = "truncate {$table_prefix}concept_nstr";
	$result_delete = mysql_query($query_delete) or die("\n<br>Query:$query\n<br>Error:".mysql_error());

	print_debug_msg("disable keys");
	$query = "alter table {$table_prefix}concept_nstr disable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());

	print_debug_msg("create nstr_cui_list");
	$nstr_cui_list = array();
	$query = "select cui, str from MRCONSO";
	$result = mysql_query($query) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
	while( $row = mysql_fetch_array($result) ) {
		$cui = $row['cui'];
		$str = $row['str'];
	
		$token_array = tokenize_string($str, array('split_digit'=>true));
		$ntoken_array = normalize_token_array($token_array, array('split_digit'=>true));
		
		$nstr = "";
		foreach ( $ntoken_array as $ntoken ) {
			if ( $nstr ) $nstr .= " ";
			$nstr .= $ntoken['str'];
		}
		if ( !trim($nstr) ) continue;
		
		$nstr_cui_list[$nstr][$cui] = true;
	}
	mysql_free_result($result);
	
	print_debug_msg("insert concept_nstr");
	foreach ( $nstr_cui_list as $nstr => $nstr_val ) {
		foreach ( $nstr_val as $cui => $val ) {
			$query_insert = sprintf("insert into {$table_prefix}concept_nstr(cui,nstr) values ('%s','%s')", 
				$cui, add_to_query($nstr));
			$result_insert = mysql_query($query_insert) or die("\n<br>Query:$query\n<br>Error:".mysql_error());
		}
	}

	print_debug_msg("enable keys");
	$query = "alter table {$table_prefix}concept_nstr enable keys";
	mysql_query($query) or die("\n<br>$query\n<br>".mysql_error());

	print_debug_msg("done");

}
print_debug_msg("<a href='install.php'>exit</a>");

function make_distinct_item_count($str)
{
	if ( !$str ) return 0;
	
	$item_list = array();
	foreach ( explode("_", substr($str, 1)) as $item ) $item_list[$item] = true;
	
	return count($item_list);
}

function make_distinct_item_str($str)
{
	if ( !$str ) return "";
	
	$item_list = array();
	foreach ( explode("_", substr($str, 1)) as $item ) $item_list[$item] = true;

	ksort($item_list);
	$out = implode("_", array_keys($item_list));
	
	return $out;
}

function make_distinct_item_sty($str)
{
	global $tui_sty_list;

	if ( !$str ) return "";
	
	$item_list = array();
	foreach ( explode("_", substr($str, 1)) as $tui ) {
		$item = $tui_sty_list[$tui];
		$item_list[$item] = true;
	}

	ksort($item_list);
	$out = implode("_", array_keys($item_list));
	
	return $out;
}
?>
<p>&nbsp;</p>
<?
include "tail.php"
?>